package com.yex.controller.modify.bank;

import com.yex.common.exception.RRException;
import com.yex.common.utils.R;
import com.yex.entity.BankEntity;
import com.yex.service.modify.bank.BankService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/appApi/bank")
public class ApiBankController {

    @Autowired
    private BankService bankService;

    @PostMapping("add")
    public R add(BankEntity bankEntity, HttpServletRequest request) {
        if (StringUtils.isBlank(bankEntity.getAddress())) throw new RRException("请输入开户地址");
        if (StringUtils.isBlank(bankEntity.getBranchAddress())) throw new RRException("请输入支行地址");
        if (StringUtils.isBlank(bankEntity.getCardNumber())) throw new RRException("请输入银行卡号");
        if (StringUtils.isBlank(bankEntity.getName())) throw new RRException("请输入开户名");
        bankService.add(bankEntity, request);
        return R.ok("添加银行卡成功");
    }

    @PostMapping("setDefault")
    public R setDefault(Integer id, HttpServletRequest request) {
        if (id == null) throw new RRException("缺少必填参数: id");
        bankService.setDefault(id, request);
        return R.ok("设置默认银行卡成功");
    }

    @PostMapping("delete")
    public R delete(Integer id, HttpServletRequest request) {
        if (id == null) throw new RRException("缺少必填参数: id");
        bankService.delete(id, request);
        return R.ok("删除银行卡成功");
    }


    @GetMapping("list")
    public R list() {
        List<BankEntity> bankEntities = bankService.findByMyList();
        return R.okData(bankEntities);
    }

}
